﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;

namespace WebFlipScreenSaver.Model
{
	public class WebPageViewModel : NotifyPropertyChangedBase
	{
		DispatcherTimer _refreshTimer = new DispatcherTimer();
		public WebPageViewModel()
		{
			_refreshTimer.Tick += new EventHandler(_refreshTimer_Tick);

		}

		void _refreshTimer_Tick(object sender, EventArgs e)
		{
			_refreshTimer.Stop();
			LastRefreshTime = DateTime.Now;
			Refresh();
			_refreshTimer.Interval = new TimeSpan(0, 0, 0, WebConfig.RefreshIntervalSecs);
			_refreshTimer.Start();
		}

		private WebPageConfigurationViewModel _webConfig;

		public WebPageConfigurationViewModel WebConfig
		{
			get { return _webConfig; }
			set
			{

				_webConfig = value;
				Changed(() => WebConfig);

				_refreshTimer.Interval = new TimeSpan(0, 0, 0, WebConfig.RefreshIntervalSecs);
				_refreshTimer.Start();

			}
		}

		private DateTime _lastRefreshTime;

		public DateTime LastRefreshTime
		{
			get { return _lastRefreshTime; }
			set
			{
				_lastRefreshTime = value;
				Changed(() => LastRefreshTime);
			}
		}

		private DateTime _displayedStartTime;

		public DateTime DisplayedStartTime
		{
			get { return _displayedStartTime; }
			set
			{
				_displayedStartTime = value;
				Changed(() => DisplayedStartTime);
			}
		}

		private int _secondsUntilRefresh;

		public int SecondsUntilRefresh
		{
			get { return _secondsUntilRefresh; }
			set
			{
				_secondsUntilRefresh = value;
				Changed(() => SecondsUntilRefresh);
			}
		}

		private int _secondsUntilPageChange;

		public int SecondsUntilPageChange
		{
			get { return _secondsUntilPageChange; }
			set
			{
				_secondsUntilPageChange = value;
				Changed(() => SecondsUntilPageChange);
			}
		}

		private bool _doRefresh;

		public bool DoRefresh
		{
			get { return _doRefresh; }
			set
			{
				_doRefresh = value;
				Changed(() => DoRefresh);
			}
		}


		internal void Refresh()
		{
			DoRefresh = !DoRefresh;

		}
	}
}
